package com.mlnx.netty.maibb.uitl;

/**
 * @Author: Akrio
 * @Date: 2021-01-14 8:54
 */
public class CodeUtil {
    /**
     * 获取设备时间新版协议
     *
     * @param byteMessage
     * @return
     */
    public static String getDeviceTime4New(byte[] byteMessage) {
        int dyear = byteMessage[25] & 0xff;
        int mouth = byteMessage[26] & 0xff;
        int day = byteMessage[27] & 0xff;
        int hour = byteMessage[28] & 0xff;
        int mis = byteMessage[29] & 0xff;
        String stringdYear = IKnetUtil.cover(dyear, 2);
        String stringMouth = IKnetUtil.cover(mouth, 2);
        String stringDay = IKnetUtil.cover(day, 2);
        String stringHour = IKnetUtil.cover(hour, 2);
        String stringMis = IKnetUtil.cover(mis, 2);
        String time = stringdYear + stringMouth + stringDay + stringHour
                + stringMis;
        return time;
    }

    /**
     * 从测量解果中获取设备编码
     *
     * @param byteMessage
     * @return
     */
    public static String getResultDevCode4New(byte[] byteMessage) {
        Integer t1 = byteMessage[11] & 0xff;
        Integer t2 = byteMessage[12] & 0xff;
        int year = byteMessage[13] & 0xff;
        int month = byteMessage[14] & 0xff;
        int batch = byteMessage[15] & 0xff;
        int no = 0;
        if (byteMessage[17] >= 0) {
            no = (byteMessage[16] & 0xff) * 256 + (byteMessage[17]);
        } else {
            no = (byteMessage[16] & 0xff) * 256 + (byteMessage[17])+ 256;
        }
        String stringt1 =null;
        if(t1<10){
            stringt1 = IKnetUtil.cover(t1, 2);
        }else{
            stringt1 = Integer.toHexString(t1).toUpperCase();
        }
        String stringt2 = null;
        if(t2<10){
            stringt2 = IKnetUtil.cover(t2, 2);
        }else{
            stringt2 = Integer.toHexString(t2).toUpperCase();
        }
        String stringYear = IKnetUtil.cover(year, 2);
        String stringMonth = IKnetUtil.cover(month, 2);
        String stringbatch = IKnetUtil.cover(batch, 2);
        String stringNo = IKnetUtil.cover(no, 4);
        String deviceNo = stringt1+stringt2+stringYear + stringMonth + stringbatch+ stringNo;
        return deviceNo;
    }

}
